#include <iostream>
#include <sys/socket.h> // 必选！socket(), bind(), listen(), accept() 等
#include <netinet/in.h> // 必选！struct sockaddr_in（IPv4 地址结构）
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/types.h> // 必选！socket(), bind(), listen(), accept() 等
#include <string.h>
#include <stdlib.h>
#include <error.h>
#include <poll.h>
#include <sys/epoll.h>
#include "protecol.h"
#include "CData.h"
#include "LoginTask.h"
#include <fcntl.h>
#include "FriendTask.h"
#include "ChatTask.h"
#include "TcpServer.h"
#include "SharedMemoryIPC.h"
using namespace std;

void initUserDate()
{
    CData::user_data = {
        {1, "user1", "123456", ":/image/image/1.jpg"},
        {2, "user2", "123456", ":/image/image/2.jpg"},
        {3, "user3", "123456", ":/image/image/3.jpg"}};
    cout << "初始化用户数据:" << CData::user_data.size() << endl;
}

int main()
{
    initUserDate();
    // TcpServer server(8888);
    // server.start();
    char buffer[1024] = {0};

    HEAD head = {0};
    char body[1024] = {0};
    int clientfd = 0;

   
    SharedMemoryIPC &write_ipc = SharedMemoryIPC::getWriterInstance(1002,2002,3002);
     SharedMemoryIPC &read_ipc = SharedMemoryIPC::getReaderInstance();

    ThreadPool pool;
    pool.setMode(PoolMode::MODE_CACHED);
    pool.start();
    while (1)
    {
        read_ipc.read_shm(buffer);
     
        memcpy(&clientfd, buffer, sizeof(clientfd));
        cout<<"接收数据 fd:"<<clientfd<<endl;
        memcpy(&head, buffer + sizeof(int), sizeof(HEAD));
        memcpy(&body, buffer + sizeof(HEAD) + sizeof(int), head.data_len);
        BaseTask *task = nullptr;
        if (head.service_type == LOGIN)
        {
            task = TaskFactory::getTask(head.service_type, body, head.data_len, clientfd);
        }
        else if (head.service_type == CHAT)
        {
            task = TaskFactory::getTask(head.service_type, body, head.data_len, clientfd);
        }
        else if (head.service_type == FRIEND)
        {
            task = TaskFactory::getTask(head.service_type, body, head.data_len, clientfd);
        }
        else if(head.service_type == CHATMSG)
        {
            task = TaskFactory::getTask(head.service_type, body, head.data_len, clientfd);
        }
        pool.submitTask([&](){ task->work(&write_ipc); });
    }

    // int sockfd = socket(AF_INET, SOCK_STREAM, 0);
    // initUserDate();
  
    // // 端口复用
    // int opt = 1;
    // setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); // 只需这一行！
    // if (sockfd < 0)
    // {
    //     perror("socket error");
    //     return 0;
    // }
    // // 绑定
    // struct sockaddr_in addr;
    // addr.sin_family = AF_INET;
    // addr.sin_addr.s_addr = inet_addr("192.168.139.128");
    // addr.sin_port = htons(8888);

    // bind(sockfd, (struct sockaddr *)&addr, sizeof(addr));

    // struct sockaddr_in clientaddr;
    // socklen_t clientlen = sizeof(clientaddr);
    // listen(sockfd, 128);

    // int epfd = epoll_create(1);
    // struct epoll_event ev;

    // struct epoll_event evs[1024] = {0};
    // ev.data.fd = sockfd;
    // ev.events = EPOLLIN;
    // epoll_ctl(epfd, EPOLL_CTL_ADD, sockfd, &ev);

    // char send_buffer[1024] = {0};
    // char body[1024] = {0};
    // HEAD head = {0};
    // int num = 0;
    // for (;;)
    // {
    //     num = epoll_wait(epfd, evs, sizeof(evs), -1); // 阻塞等待
    //     if (num < 0)
    //     {
    //         perror("epoll wait error");
    //         continue;
    //     }
    //     for (int i = 0; i < num; i++)
    //     {
    //         if (evs[i].data.fd == sockfd)
    //         {
    //             int client_fd = accept(sockfd, NULL, NULL);
    //             cout << "有新客户端连接" << endl;
    //             struct epoll_event ev_c;
    //             ev_c.data.fd = client_fd;
    //             ev_c.events = EPOLLIN;
    //             epoll_ctl(epfd, EPOLL_CTL_ADD, client_fd, &ev_c);
    //         }
    //         else if (evs[i].events == EPOLLIN)
    //         {
    //             bzero(&head, sizeof(HEAD));
    //             bzero(body, sizeof(body));
    //             int clientfd = evs[i].data.fd;
    //             int size = read(clientfd, &head, sizeof(HEAD));
    //             if (size == 0)
    //             {
    //                 close(clientfd);
    //                 epoll_ctl(epfd, EPOLL_CTL_DEL, clientfd, NULL);
    //             }

    //             size = read(clientfd, body, head.data_len);
    //             if (size == 0)
    //             {
    //                 close(clientfd);
    //                 epoll_ctl(epfd, EPOLL_CTL_DEL, clientfd, NULL);
    //             }
    //             cout << "head.service_type =" << head.service_type << endl;
    //             if (head.service_type == LOGIN)
    //             {

    //                 BaseTask*task=new LoginTask(body,size,clientfd);
    //                 task->work();
    //             }
    //             else if (head.service_type == CHAT)
    //             {
    //                 BaseTask*task=new ChatTask(body,size,clientfd);
    //                 task->work();

    //             }
    //             else if (head.service_type == FRIEND)
    //             {
    //                 BaseTask*task=new FriendTask(body,size,clientfd);
    //                 task->work();

    //             }
    //         }
    //     }
    // }
}